<?php
namespace app\admin\controller;
use think\Db;
class Welfare extends Base {
    /**
     * 首页列表
     */
    public function index(){
        if(request()->isAjax()){
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('my_welfare')->alias('a')
                ->join('up_admin ua','ua.id = a.welfare_release_id','left')
                ->join('up_mechanism um','um.mechanism_id = a.mechanism_id','left')
                ->count();//计算总页面
            $list = Db::name('my_welfare')->alias('a')
                ->join('up_admin ua','ua.id = a.welfare_release_id','left')
                ->join('up_mechanism um','um.mechanism_id = a.mechanism_id','left')
                ->order('a.create_time desc')
                ->field('a.*,ua.username,um.mechanism_name')
                ->page($page, $limit)
                ->select();//计算总页面
            foreach ($list as $key=>&$vo){
                $vo['num'] = count(explode(',',trim($vo['user_id'],',')));
            }
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(session('?welfare_id')){
            session('welfare_id',null);
        }
        return $this->fetch();
    }

    /**
     *发布荣誉
     */
    public function add(){
        if(request()->isAjax()){
            $data['welfare_content'] = input('param.detail');
            $data['user_id'] = ','.implode(',',input('param.user_id/a')).',';
            $data['welfare_title'] = input('param.title');
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $data['welfare_release_id'] = session('uid');
            $data['mechanism_id'] = 1;
            $aaa = Db::table('up_my_welfare')->insertGetId($data);
            if($aaa){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $whereMap['a.user_to_mechanism_id'] = array('>',1);
        $list = Db::table('up_u_user')->alias('a')
            ->join('up_mechanism um','um.mechanism_id = a.user_to_mechanism_id','left')
            ->field('a.user_id,a.user_username,um.mechanism_name')
            ->where($whereMap)
            ->select();
        $this->assign('list',$list);

        return $this->fetch();
    }
    /**
     * 删除荣誉
     */
    public function delete(){
        $ids = input('param.ids');
        Db::startTrans();// 启动事务
        $res = Db::table('up_my_welfare')->where('welfare_id',$ids)->delete();
        if($res){
            Db::commit();
            writelog(session('uid'),session('username'),'删除成功',1);
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            Db::rollback();
            writelog(session('uid'),session('username'),'删除失败',2);
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    /**
     * 编辑
     */
    public function update(){
        if(request()->isAjax()){
            $id = input('param.id');
            $data['welfare_content'] = input('param.detail');
            $data['user_id'] = ','.implode(',',input('param.user_id/a')).',';
            $data['welfare_title'] = input('param.title');
            $data['welfare_release_id'] = session('uid');
            $res = Db::table('up_my_welfare')->where('welfare_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $res = Db::table('up_my_welfare')->where('welfare_id',$id)->find();
        $res['user_list'] = explode(',',trim($res['user_id'],','));
        $whereMap['a.user_to_mechanism_id'] = array('>',1);
        $list = Db::table('up_u_user')->alias('a')
            ->join('up_mechanism um','um.mechanism_id = a.user_to_mechanism_id','left')
            ->field('a.user_id,a.user_username,um.mechanism_name')
            ->where($whereMap)
            ->select();
        $this->assign('list',$list);
        $this->assign('res',$res);
        return $this->fetch();
    }

    /**
     * 查看获得人
     */
    public function detail(){

            $id = input('param.id');
            $res = Db::table('up_my_welfare')->where('welfare_id',$id)->find();
            $user_list = explode(',',trim($res['user_id'],','));
            $list = array();
            foreach ($user_list as $key=>$vo){
                $result = Db::table('up_u_user')->where('user_id',$vo)->field('user_username,user_phone,user_rel_name')->find();
                $list[$key]= $result;
            }
        $this->assign('list',$list);
        return $this->fetch();
    }
}